package com.lwb.ztx.news.widget;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.bean.NewsSlideBean;
import com.lwb.ztx.database.GreenDaoManager;
import com.lwb.ztx.database.greendao.dao.CollectDao;
import com.lwb.ztx.database.greendao.db.Collect;
import com.lwb.ztx.news.presenter.NewsImagePresenter;
import com.lwb.ztx.news.presenter.NewsImagePresenterImpl;
import com.lwb.ztx.news.view.NewsImageView;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.ListUtils;
import com.lwb.ztx.utils.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import uk.co.senab.photoview.PhotoViewAttacher;

public class NewsImageActivity extends SwipeBackActivity implements NewsImageView, ViewPager
        .OnPageChangeListener, View.OnClickListener {

    private ImageView imageView;
    private NewsBean newsBean;
    private PhotoViewAttacher mAttacher;

    private TextView textView;
    private ViewPager viewPager;
    private NewsSliderAdapter adapter;

    private NewsImagePresenter presenter;

    private TextView descriptionText;
    private TextView indexText;

    private View backView;
    private CheckBox collectBtn;

    List<NewsSlideBean> beans;

    CollectDao dao;
    Collect collect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new NewsImagePresenterImpl(this);
        initIntent();
        if (newsBean.getType().equals("shortNews")) {
            setContentView(R.layout.activity_news_image);
            imageView = (ImageView) this.findViewById(R.id.news_image_screen);
            ImageLoaderUtils.display(this, imageView, newsBean.getThumbnail());
            mAttacher = new PhotoViewAttacher(imageView);
        } else if (newsBean.getType().equals("slide")) {
            setContentView(R.layout.activity_news_slide);
            textView = (TextView) this.findViewById(R.id.slide_title);
            descriptionText = (TextView) this.findViewById(R.id.slide_description);
            indexText = (TextView) this.findViewById(R.id.slide_index);
            textView.setText(newsBean.getTitle());
            viewPager = (ViewPager) this.findViewById(R.id.slide_viewpager);
            adapter = new NewsSliderAdapter(this.getSupportFragmentManager());
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(this);
            presenter.getSlideImages(newsBean.getLinkUrl());
        }
        backView = this.findViewById(R.id.back);
        backView.setOnClickListener(this);
        collectBtn = (CheckBox) this.findViewById(R.id.collect);
        collectBtn.setOnClickListener(this);
        collectBtn.setChecked(collect != null);
        if (newsBean.getType().equals("shortNews")) {
            collectBtn.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != viewPager) {
            viewPager.removeOnPageChangeListener(this);
        }
    }

    private void initIntent() {
        newsBean = (NewsBean) getIntent().getSerializableExtra("news");
        LogUtils.i(newsBean.toString());
        dao = GreenDaoManager.getDaoSession(this).getCollectDao();
        collect = dao.load(newsBean.getDocumentId());
    }

    @Override
    public void setSlideImages(List<NewsSlideBean> beans) {
        List<Fragment> views = new ArrayList<>();
        this.beans = beans;
        for (int i = 0; i < beans.size(); i++) {
            NewsImageFragment fragment = new NewsImageFragment(beans.get(i).getImage());
            views.add(fragment);
        }
        if (beans.size() > 0) {
            indexText.setText("1/" + beans.size());
            descriptionText.setText(beans.get(0).getDescription());
            textView.setText(beans.get(0).getTitle());
        }
        adapter.setFragments(views);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        descriptionText.setText(beans.get(position).getDescription());
        indexText.setText((position + 1) + "/" + beans.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.collect:
                LogUtils.i(newsBean.toString());
                if (collectBtn.isChecked()) {
                    if (collect == null) {
                        collect = new Collect();
                        String docId = newsBean.getDocumentId().equals("") ? newsBean
                                .getId() : newsBean.getDocumentId();
                        collect.setCollectDocId(docId);
                        if (ListUtils.getSize(beans) != 0 && beans.get(0) != null) {
                            collect.setCollectImageUrl(beans.get(0).getImage());
                        }
                        collect.setCollectTime(new Date());
                        collect.setCollectTitle(newsBean.getTitle());
                        collect.setCollectType(newsBean.getType());
                        collect.setCollectUpdateTime(newsBean.getFormatTime());
                        collect.setCollectLinkUrl(newsBean.getLinkUrl());
                        collect.setCollectSource(newsBean.getSource());
                        dao.insert(collect);
                        Toast.makeText(this, "收藏成功！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    dao.delete(collect);
                    collect = null;
                    Toast.makeText(this, "取消收藏！", Toast.LENGTH_SHORT).show();
                }
                LogUtils.i(dao.loadAll().toString());
                break;
        }
    }
}
